import { pinyin } from 'pinyin-pro'

export function formatShopData(shopList) {
  const map = {}
  shopList.forEach((shop) => {
    if (shop.type === 0) {
      shop.name += '（供应商）'
    }
    shop.name = shop.name.trim() // 去除首尾空格

    // 获取拼音首字母（大写）
    const firstChar = pinyin(shop.name, {
      toneType: 'none',
      type: 'array',
    })[0]?.[0]
      .charAt(0)
      .toUpperCase()

    // 判断是否是 A-Z 字母
    const groupKey = /^[A-Z]$/.test(firstChar) ? firstChar : '#'

    if (!map[groupKey]) {
      map[groupKey] = []
    }

    map[groupKey].push(shop)
  })

  // 将对象转为数组并排序：先字母，再 #
  const sortedKeys = Object.keys(map).sort((a, b) => {
    if (a === '#')
      return 1
    if (b === '#')
      return -1
    return a.localeCompare(b) // 按字母顺序排序
  })

  return sortedKeys.map(key => ({
    index: key,
    list: map[key],
  }))
}
